package com.itheima.comtroller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService_lzl;
import com.itheima.utils.AES;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;
/*
* 会员
* */
@RestController
@RequestMapping("/member_lzl")
public class MemberController_lzl {
    @Reference
    private MemberService_lzl memberService_lzl;
    @Autowired
    private JedisPool jedisPool;
    /*
    * 1.判断验证码是否正确
    * 2.调用service，查询会员，判断该用户是否是会员，不是的话，自动注册会员
    * 3.将用户存入jedis中
    *   将用户存入cookie中，用于客户端跟踪用户
    * */
    @RequestMapping("/login")
    public Result login(HttpServletResponse response,@RequestBody Map map){
        String validateCode = (String) map.get("validateCode");
        String telephone = (String) map.get("telephone");
        String codeLogin = jedisPool.getResource().get( RedisMessageConstant.SENDTYPE_LOGIN+telephone);
        System.out.println("验证码"+codeLogin);
        if(telephone!=null&&codeLogin!=null&&validateCode.equals(codeLogin)){
            //验证码正确

            try {
                Member member = memberService_lzl.findMembertBytelephone(telephone);
                if(member==null){
                    //不会会员
                    //注册会员
                    member = new Member();
                    member.setPhoneNumber(telephone);
                    member.setRegTime(new Date());
                    memberService_lzl.add(member);
                }
                //存cookie，用户客户端跟踪用户
                //使用AES進行加密
                String encrypt = AES.aesEncrypt(telephone, AES.KEY);
                Cookie cookie = new Cookie("login_telephone",encrypt);
                cookie.setPath("/");
                cookie.setMaxAge(60*60*24*30);
                response.addCookie(cookie);
                //将该用户存入jedis中
                jedisPool.getResource().setex(telephone,30*60, JSON.toJSON(member).toString());

                return new Result(true,MessageConstant.LOGIN_SUCCESS);
            } catch (Exception e) {
                e.printStackTrace();
                return new Result(false,MessageConstant.ADD_MEMBER_FAIL);
            }


        }else {
            //验证码错误
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
    }

    @RequestMapping("/findMember")
    public Result findMember(HttpServletRequest request) throws Exception {
        Cookie[] cookies = request.getCookies();
        if(cookies!=null&&cookies.length>0){
            for (Cookie cookie : cookies) {
                String name = cookie.getName();
                if("login_telephone".equals(name)){
                    String value = cookie.getValue();
                    //使用AES进行解密
                    String decrypt = AES.aesDecrypt(value, AES.KEY);
                    String s = jedisPool.getResource().get(decrypt);
                    Member member = JSON.parseObject(s, Member.class);

                    return new Result(true,"",member);
                }

            }
        }
      return new Result(false,"获取会员信息失败！");
    }
    @RequestMapping("/submitUpdate")
    public Result submitUpdate(@RequestBody Member member,HttpServletResponse response)   {
        try {
            String phoneNumber = member.getPhoneNumber();
            memberService_lzl.submitUpdate(member);
            String encrypt = AES.aesEncrypt(phoneNumber, AES.KEY);
            Cookie cookie = new Cookie("login_telephone", encrypt);
            cookie.setPath("/");
            cookie.setMaxAge(60*60*24*30);
            response.addCookie(cookie);
            //将该用户存入jedis中
            jedisPool.getResource().setex(phoneNumber,30*60, JSON.toJSON(member).toString());
            return new Result(true,"修改会员成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改会员失败！");
        }
    }
}
